package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.orderservice.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志管理 前端控制器
 * </p>
 *
 * @author kaixuan
 * @since 2020-06-13
 */
@Api(description = "支付日志管理")
@RestController
@RequestMapping("/orderservice/log")
//@CrossOrigin
public class TPayLogController {

    @Autowired
    private TPayLogService payLogService;

    /**
     * 根据订单号生成微信支付二维码
     * @param orderNo 订单号
     * @return
     */
    @ApiOperation("根据订单号生成微信支付二维码")
    @GetMapping("payCode/{orderNo}")
    public R payCode(@ApiParam(value = "订单号",name = "orderNo",required = true)@PathVariable String orderNo){
        Map<String,Object> map = payLogService.payCode(orderNo);
        return R.ok().data("map",map);
    }

    /**
     * 根据订单号获取订单支付状态
     * @param orderNo 订单号
     * @return
     */
    @ApiOperation("根据订单号获取订单支付状态")
    @GetMapping("payStatus/{orderNo}")
    public R payStatus(@ApiParam(value = "订单号",name = "orderNo",required = true)@PathVariable String orderNo){
        //获取微信第三方接口返回的数据
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        if (null == map){
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")){
            //支付成功 更改订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中");
    }
}

